#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os, sys
from xml.dom import minidom

# data input/out 
APPID_FILE = 'app_id'
JOBSID_FILE = 'jobs_id'
TASKID_FILE = 'tasks_id'

# Gateway config. stuff.
# GATEWAY_URL = 'http://192.168.28.220/jobtracker?wsdl'
GATEWAY_URL = 'http://192.168.28.221/jobtracker?wsdl'
# GATEWAY_URL = 'http://192.168.28.222/jobtracker?wsdl'
# GATEWAY_URL = 'http://192.168.28.216/jobtracker?wsdl'
# GATEWAY_URL = 'http://192.168.28.217/jobtracker?wsdl'
# GATEWAY_URL = 'http://192.168.28.237/jobtracker?wsdl'
LOC_WSDL_FILE = 'file://' + os.path.expanduser('~/jobtracker.wsdl')
# 

# {REGISTER}
__res_require_dict = {
    'cpuMHz': '2000',
    'cpuUsed': '80',
    'memoryFreeMb': '1024',
    'diskFreeMb': '500',
    'os': 'ubuntu,redhat'
}

RES_REQUIRE_TEMP = '''<?xml version="1.0" encoding = "utf-8"?>
<require>
    <cpuMHz attribute="GT">%(cpuMHz)s</cpuMHz>
    <cpuUsed attribute="LT">%(cpuUsed)s</cpuUsed>
    <memoryFreeMb attribute="GT">%(memoryFreeMb)s</memoryFreeMb>
    <diskFreeMb>%(diskFreeMb)s</diskFreeMb>
    <os attribute="or">%(os)s</os>
</require>
''' % __res_require_dict

def generate_app_register_dict():
    app_reg_dict = {
        'appName': '',
        'appDisplayName': '',
        'username': 'wang_an',
        'password': 'wang_an',
        
        # dummy XXXX
        'apps': {
            'appContent': '',    # app file ball content, base64
            'appFileName': '',
            'appFileMd5Value': '',    # file md5 above
            'scriptContent': '',    # base64
            'scriptFileName': '',
            'scriptMd5Value': '',
            'resourceRequire': ''    # xml info.                
        }
    }
    return app_reg_dict

# {SUBMIT}
# JOB_PARAMS_TEMP = '''<?xml version="1.0" encoding = "utf-8"?>
# <parameters>
#     <parameter>
#         <content>%s</content>
#     </parameter>
# </parameters>
# '''

JOB_PARAMS_TEMP = r'''<?xml version="1.0" encoding = "utf-8"?>
<parameters>
</parameters>
'''

def generate_job_submit_dict(params):
    app_sub_dict = {
        'appId': '', # string, app. id returned by register invoke.
        'userJobId': 'pwpd_crawler_wangan', # string, 
        'parameter': '', # xml, params for job script.
        'resultAddress': '', # url, result addr.
        'jobPriority': 0,   # 0:vip, 1-10: general
        'copyNumber': 2,
        'budgetRunTime': 144, # min.
        'username': 'wang_an',
        'password': 'wang_an',
        'resourceProvince': 2, # -1: not assigned; 1: beijing; 2: shanghai;
        'ispName': -1,  # 1: telecom; 2: unicom; 3: cmobile; 4: edu. net; 5: tietong
                        # -1: not assigned
        'Bandwidth': 'EQ|10' # string
    }
    
    xmldoc = minidom.parseString(JOB_PARAMS_TEMP)
    parameters = xmldoc.getElementsByTagName('parameters')[0]

    for p_file in params:
        try:
            p_fd = file(os.path.expanduser(p_file), 'r')
            
            content = xmldoc.createElement('content')
                 
            val = xmldoc.createTextNode(p_fd.read())
            content.appendChild(val)
                 
            parameter = xmldoc.createElement('parameter')
            parameter.appendChild(content)
                 
            parameters.appendChild(parameter)

        except Exception, e:
            print >> sys.stderr, 'read raw data file [%s] error, exit: %s' %(p_file, str(e))
            exit(1)

    app_sub_dict['parameter'] = xmldoc.toxml('utf-8').decode('utf-8')
    return app_sub_dict

# {KILL} & {QUERY}
def generate_job_opt_dict():
    params = {
        'userJobId': '',
        'username': 'wang_an',
        'passwrod': 'wang_an'              
    }
    
    return params

if __name__ == '__main__':
    
    fname = sys.argv[1:]
    print generate_job_submit_dict(fname)['parameter']
    